package com.test.common;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import msg.chestionar.ejb.bi.ChestionarBI;
import msg.chestionar.ejb.bi.RaspunsBI;

import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.UtilizatorTO;

@ManagedBean(name = "completeazaChBean")
@SessionScoped
public class CompleteazaChBean {

	private ChestionarTO chestionar;

	@EJB
	private ChestionarBI chestionarBI;

	@EJB
	private RaspunsBI raspunsBI;


	public ChestionarTO getChestionar() {

		if (chestionar == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			String chestionarId = (String) context.getExternalContext()
					.getRequestParameterMap().get("chestionarSelected");

			if (chestionarId != null)
				this.chestionar = chestionarBI.getChestionar(Long
						.valueOf(chestionarId));
		}
		return chestionar;
	}

	public void setChestionar(ChestionarTO chestionar) {
		this.chestionar = chestionar;
	}

	public String saveRaspunsuri() {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext()
				.getSession(false);
		UtilizatorTO user = (UtilizatorTO) session.getAttribute("loggedInUser");

		raspunsBI.saveRaspunsuri(chestionar, user);
		this.chestionar = null;
		return "thanx";
	}

}
